﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Agenda : System.Web.UI.Page
{
    PlanningManager pm;
    MovieManager mm;
    DateTimeConverter dtc = new DateTimeConverter();

    DateTime date = DateTime.Today;
    int selectedIndex = 0;
    string language = "dutch";

    protected void Page_Load(object sender, EventArgs e)
    {
        pm = new PlanningManager((debioscoopEntities)Session["db"]);
        mm = new MovieManager((debioscoopEntities)Session["db"]);

        language = Master.tl.getCurrentLanguage();

        if (!IsPostBack)
        {
            BindListView();
        }
    }

    private void BindListView()
    {
        //Get date en selectedIndex from dropdownlist if post back
        if (IsPostBack)
        {
            selectedIndex = ddlPlanning.SelectedIndex;
            date = new DateTime(int.Parse(ddlPlanning.SelectedValue.Substring(6, 4)), int.Parse(ddlPlanning.SelectedValue.Substring(3, 2)), int.Parse(ddlPlanning.SelectedValue.Substring(0, 2)));
        }

        movieList.DataSource = mm.getMoviesByDate(date, (date == DateTime.Today ? true : false));
        movieList.DataBind();

        //Loop trough each item in the movieList 
        foreach (ListViewItem repeateritem in movieList.Items)
        {
            //Get movieID from hiddenfield
            int movieID = int.Parse(((HiddenField)repeateritem.FindControl("movieID")).Value);

            //Bind movie times to planningrepeater
            ((Repeater)(repeateritem.FindControl("movieListTimes"))).DataSource = pm.getPlanningenMovieByDate(movieID, date, (date == DateTime.Today ? true : false));
            ((Repeater)(repeateritem.FindControl("movieListTimes"))).DataBind();
        }

        ddlPlanning.DataSource = dtc.getDatesFromNow(language);
        ddlPlanning.DataTextField = "value";
        ddlPlanning.DataValueField = "key";
        ddlPlanning.SelectedIndex = selectedIndex;
        ddlPlanning.DataBind();
    }

    protected void movieList_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        DataPager movieDataPager = movieList.FindControl("movieDataPager") as DataPager;

        //set current page startindex, max rows and rebind to false
        movieDataPager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

        //rebind List View
        BindListView();
    }

    protected void movieList_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        film movie = (film)e.Item.DataItem;

        //Fill label with Movie summary
        Label lblMovieSummary = (Label)e.Item.FindControl("lblMovieSummary");

        lblMovieSummary.Text = mm.getSummary(movie, language);

        //Fill label with Movie genre
        Label movieGenre = (Label)e.Item.FindControl("movieGenre");
        movieGenre.Text = mm.getGenreInString(movie, "|", language, true);


    }

    protected void movieListTimes_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Button btnMovieTime = (Button)e.Item.FindControl("btnMovieTime");
        planning tempPlanning = (planning)e.Item.DataItem;
        btnMovieTime.Text = dtc.getTimeforMovie(tempPlanning.tijd) + ((bool)tempPlanning.dried ? " (3D)" : "");
    }

    protected void ddlPlanning_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Reset datapager is available
        DataPager movieDataPager = movieList.FindControl("movieDataPager") as DataPager;
        if (movieDataPager != null)
        {
            //set current page startindex, max rows and rebind to false
            movieDataPager.SetPageProperties(0, movieDataPager.MaximumRows, false);
        }
        BindListView();
    }

    protected void btnMovieTime_Click(object sender, EventArgs e)
    {
        Button btnMovieTime = (Button)sender;
        RepeaterItem movieListTimes = (RepeaterItem)btnMovieTime.NamingContainer;
        HiddenField planningID = (HiddenField)movieListTimes.FindControl("planningID");

        planning sessionPlanning = pm.getPlanning(int.Parse(planningID.Value));
        Session["planning"] = sessionPlanning;
        Response.Redirect("~/bestellen/default.aspx?id=" + sessionPlanning.filmfilm_id);
    }

    protected void movieDataPager_Init(object sender, EventArgs e)
    {
        translateDataPager((DataPager)sender);
    }

    protected void movieDataPage2_Init(object sender, EventArgs e)
    {
        translateDataPager((DataPager)sender);
    }

    public void translateDataPager(DataPager tempDataPager)
    {
        NextPreviousPagerField field;
        field = (NextPreviousPagerField)tempDataPager.Fields[0];
        field.NextPageText = Master.tl.getTranslation("next");
        field.PreviousPageText = Master.tl.getTranslation("previous");

        field = (NextPreviousPagerField)tempDataPager.Fields[2];
        field.NextPageText = Master.tl.getTranslation("next");
        field.PreviousPageText = Master.tl.getTranslation("previous");
    }
}